#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_COLS 100
#define MAX_ROWS 100


int add_line(int time) 
{
    FILE *file = fopen("data.csv", "a"); // 以追加模式打开文件
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    // 要写入的数据
    char data[] = "John,Doe,30\n";

    // 将数据写入文件
    fputs(data, file);
    printf("-----time = %d ------\n",time);
    fclose(file);
    return 0;
}


int main() {
    FILE *file = fopen("data.csv", "r");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    char line[1024];
    char *token;
    char *row[MAX_ROWS];
    int row_count = 0;

    while (fgets(line, sizeof(line), file)) {
        row[row_count] = strdup(line);
        row_count++;
    }

    for (int i = 0; i < row_count; i++) {
        int col_count = 0;
        char *col[MAX_COLS];
        token = strtok(row[i], ",");
        while (token != NULL) {
            col[col_count] = token;
            col_count++;
            token = strtok(NULL, ",");
        }
        for (int j = 0; j < col_count; j++) {
            printf("%s ", col[j]);
        }
        printf("\n");
    }

    fclose(file);
    printf("-----luke0043 add line for test ------\n");
    for(int i = 0;i<30 ;i++)
    {
        add_line(i);
    }
    printf("-----luke0043 add line for over ------\n");
    return 0;
}